package com.vgemv.jsutilitysdkdemo.module.List.Chat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdkdemo.module.List.WordLive.WordLiveViewVm;
import com.vgemv.jsutilitysdkdemo.Utility.CircleTransform;

public class ChatItemView extends JsMenuItemView {


    private RelativeLayout mrl_root;
    private TextView mtv_context;
    private TextView mtv_time;
    private ImageView miv_header;
    private TextView mtv_author;

    public ChatItemView(Context context) {
        super(context);
    }


    @Override
    public void customSetVm(Context context, JsMenuItemVm vm) {
        ChatItemViewVm chatVm = (ChatItemViewVm) vm;
        setBackgroundColor(Color.argb(255,220,220,220));
        //由于image要动态跟新,每次都是使用新的根视图
        {
            if(mrl_root == null) {
                mrl_root = new RelativeLayout(context);
                mrl_root.setId(View.generateViewId());
                mrl_root.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
                addView(mrl_root);
            }
        }

        {
            //iv
            if(miv_header == null) {
                miv_header = new ImageView(context);
                miv_header.setId(View.generateViewId());
                mrl_root.addView(miv_header);
            }
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( ChatItemViewVm.headerImageViewSize, ChatItemViewVm.headerImageViewSize);
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.setMargins(ChatItemViewVm.headerImageViewLeftPadding,ChatItemViewVm.headerImageViewTopPadding,0, 0);
                miv_header.setLayoutParams(lp);

                //内容
                String imageUrlString = chatVm.getModel().headimgurl;
                Picasso.get().load(imageUrlString)
                        .placeholder(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                        .error(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                        .transform(new CircleTransform())
                        .into(miv_header);


        }
        {
            //author
            if(mtv_author == null) {
                mtv_author = new TextView(context);
                mtv_author.setId(View.generateViewId());
                mrl_root.addView(mtv_author);
            }

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.RIGHT_OF,miv_header.getId());
            lp.setMargins(ChatItemViewVm.headerImageViewContentSpacing, ChatItemViewVm.timeTopPadding,0, 0);
            mtv_author.setLayoutParams(lp);

            mtv_author.setText(chatVm.getModel().discussTeminalUserName);
            mtv_author.setGravity(Gravity.CENTER);
            mtv_author.setTextColor(ChatItemViewVm.timeTextColor);
            mtv_author.setTextSize(ChatItemViewVm.authorTextSize);
            mtv_author.setBackgroundColor(Color.argb(255,255,255,255));

        }
        {
            //time
            if(mtv_time == null) {
                mtv_time = new TextView(context);
                mtv_time.setId(View.generateViewId());
                mrl_root.addView(mtv_time);
            }

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.setMargins(0, ChatItemViewVm.timeTopPadding,chatVm.timeRightPadding, 0);
            mtv_time.setLayoutParams(lp);

            mtv_time.setText(chatVm.getModel().createDatetime.substring(5,10));
            mtv_time.setGravity(Gravity.CENTER);
            mtv_time.setTextColor(ChatItemViewVm.timeTextColor);
            mtv_time.setTextSize(ChatItemViewVm.authorTextSize);
            mtv_time.setBackgroundColor(Color.argb(255,255,255,255));
        }

        {
//            content
            if(mtv_context == null) {
                mtv_context = new TextView(context);
                mtv_context.setId(View.generateViewId());
                mrl_root.addView(mtv_context);
            }

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(chatVm.contextWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.BELOW,mtv_author.getId());
                lp.addRule(RelativeLayout.RIGHT_OF,miv_header.getId());
                lp.setMargins(ChatItemViewVm.headerImageViewContentSpacing,ChatItemViewVm.timeTopPadding,0, WordLiveViewVm.itemViewContentImageVMargin);
            mtv_context.setLayoutParams(lp);

            mtv_context.setText(chatVm.getModel().discussContent);
            mtv_context.setTextColor(Color.argb(255,0,0,0));
//            mtv_context.setBackgroundColor(Color.argb(255,255,255,255));
            mtv_context.setTextSize(TypedValue.COMPLEX_UNIT_SP,ChatItemViewVm.contentTextSize);
        }
    }

    @Override
//    protected void dispatchDraw(Canvas canvas) {
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(ChatItemViewVm.timeTextColor);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(2);

            ChatItemViewVm vm = (ChatItemViewVm)mvm;
            int cornerRadius = ChatItemViewVm.contextCornerRadius;

            Path path = new Path();
            //起点
            int x0 = ChatItemViewVm.headerImageViewLeftPadding + ChatItemViewVm.headerImageViewSize + ChatItemViewVm.headerImageViewContentSpacing/3;
            int y0 = ChatItemViewVm.timeTopPadding/2;
            path.moveTo(x0,y0);

            //右上角
            int x1 = x0 + vm.contextWidth + ChatItemViewVm.contentRightPadding/2 + 2 * ChatItemViewVm.headerImageViewContentSpacing/3;
            int y1 = y0;


            //右上角左边的点.
            int x11 = x1 -  cornerRadius;
            int y11 = y1;
             path.lineTo(x11,y11);
//
//            int x12 = x1;
//            int y12 = y1 + cornerRadius;

        RectF rect = new RectF();
        rect.left = x1 - cornerRadius * 2;
        rect.right = x1;
        rect.top= y0;
        rect.bottom = y0 + cornerRadius * 2;
            path.arcTo(rect,-90,90);


            //右下角上面的点
        int rectHeight = this.getMeasuredHeight()- ChatItemViewVm.timeTopPadding/2 - ChatItemViewVm.contentBottomPadding/2;
        int x2 = x1;
        int y2 = y1 + rectHeight - cornerRadius;
        path.lineTo(x2,y2);

         rect = new RectF();
        rect.left = x1 - cornerRadius * 2;
        rect.right = x1;
        rect.top= y2 - cornerRadius;
        rect.bottom = y2 + cornerRadius;
        path.arcTo(rect,0,90);


        //左下角
        int x3 =  ChatItemViewVm.headerImageViewLeftPadding + ChatItemViewVm.headerImageViewSize + 2 * ChatItemViewVm.headerImageViewContentSpacing/3;
        int y3 = y2 + cornerRadius;

        //左下角右边的点
            int x31 = ChatItemViewVm.headerImageViewLeftPadding + ChatItemViewVm.headerImageViewSize + 2 * ChatItemViewVm.headerImageViewContentSpacing/3 + cornerRadius;
            int y31 = y3;
        path.lineTo(x31,y31);

        rect = new RectF();
        rect.left = x31 - cornerRadius;
        rect.right = x31 + cornerRadius;
        rect.top= y31 - cornerRadius * 2;
        rect.bottom = y31;
        path.arcTo(rect,90,90);


        //左上角下面的点
        int x4 = x3;
        int y4 = y0 + ChatItemViewVm.headerImageViewContentSpacing/3;
        path.lineTo(x4,y4);

        path.close();

        canvas.drawPath(path,paint);

        paint.setColor(Color.argb(255,255,255,255));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path,paint);

    }

}
